import axios from "axios";
import { message } from "ant-design-vue";
import { GetToken } from "../utils/Token";
const req = axios.create({
  // headers[]=""
  baseURL: "http://localhost:9020/Chat/",
  // timeout: 5000,
  responseEncoding: "utf8",
});

req.interceptors.request.use((confing: any) => {
  confing.headers["token"] = GetToken();

  return confing;
});
req.interceptors.response.use(
  (response: any) => {
    return response.data;
  },
  (error: any) => {
    message.error(error.message);
  }
);

const Axios = (url: string, data?: object, method = "POST") => {
  const res = req({
    url: url,
    data: data,
    method: method,
  });
  return res;
};

// class Axios {
//   static req = axios.create({
//     // headers[]=""
//     baseURL: "http://localhost:9020/Chat/",
//     // timeout: 5000,
//     responseEncoding: "utf8",
//   });

//   static init(url: string, data?: object, method = "POST") {
//     this.req.interceptors.request.use((confing: any) => {
//       console.log(confing);
//       return confing;
//     });
//     this.req.interceptors.response.use(
//       (response: any) => {
//         console.log(response);

//         // const res = JSON.parse(JSON.stringify(response.data));
//         return response;
//       },
//       (error: any) => {
//         console.log(error);

//         message.error(error.message);
//       }
//     );
//     const res = this.req({
//       url: url,
//       data: data,
//       method: method,
//     });
//     return res;
//   }
// }

export default Axios;
